/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.xd.manager.impl;

import java.time.LocalDate;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.xy.biz.xd.domain.po.DocumentReleasePO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.biz.xd.domain.bo.DocumentReleaseBO;
import com.xy.biz.xd.domain.query.DocumentReleaseQuery;
import com.xy.biz.xd.domain.converter.DocumentReleaseConverter;
import com.xy.biz.xd.manager.DocumentReleaseManager;
import com.xy.biz.xd.dao.DocumentReleaseDao;
import org.springframework.util.CollectionUtils;

import static java.util.stream.Collectors.toList;

/**
 *
 *
 * @author wcs
 * @date 2021-3-4
 */
@Service
public class DocumentReleaseManagerImpl implements DocumentReleaseManager {

    @Autowired
    private DocumentReleaseDao documentReleaseDao;

    @Override
    public int saveDocumentRelease(DocumentReleaseBO documentReleaseBO) {
        return documentReleaseDao.saveDocumentRelease(DocumentReleaseConverter.toPO(documentReleaseBO));
    }

    @Override
    public int saveAllDocumentRelease(Collection<DocumentReleaseBO> collection) {
        return documentReleaseDao.saveAllDocumentRelease(DocumentReleaseConverter.toPO(collection));
    }

    @Override
    public int updateDocumentRelease(DocumentReleaseBO documentReleaseBO) {
        return documentReleaseDao.updateDocumentRelease(DocumentReleaseConverter.toPO(documentReleaseBO));
    }

    @Override
    public int updateAllDocumentRelease(Collection<DocumentReleaseBO> collection) {
        return documentReleaseDao.updateAllDocumentRelease(DocumentReleaseConverter.toPO(collection));
    }

    @Override
    public int updateDocumentReleaseEffectDateById(DocumentReleaseBO documentReleaseBO) {
        return documentReleaseDao.updateDocumentReleaseEffectDateById(DocumentReleaseConverter.toPO(documentReleaseBO));
    }

    @Override
    public int updateDocumentReleaseForFlow(DocumentReleaseQuery query) {
        return documentReleaseDao.updateDocumentReleaseForFlow(query);
    }

    @Override
    public int deleteDocumentRelease(Long documentReleaseId) {
        return documentReleaseDao.deleteDocumentRelease(documentReleaseId);
    }

    @Override
    public DocumentReleaseBO getDocumentRelease(Long documentReleaseId) {
        return DocumentReleaseConverter.fromPO(documentReleaseDao.getDocumentRelease(documentReleaseId));
    }

    /**
     * 查找一个
     *
     * @param documentCode
     * @return
     */
    @Override
    public List<DocumentReleaseBO> listDocumentReleaseByDocumentCode(String documentCode) {
        return DocumentReleaseConverter.fromPO(documentReleaseDao.listDocumentReleaseByDocumentCode(documentCode));
    }

    /**
     * 查找一个
     *
     * @param documentReleaseQuery
     * @return
     */
    @Override
    public DocumentReleaseBO getDocumentRelease(DocumentReleaseQuery documentReleaseQuery) {
        return DocumentReleaseConverter.fromPO(documentReleaseDao.getDocumentRelease1(documentReleaseQuery));
    }

    @Override
    public List<DocumentReleaseBO> listDocumentReleaseById(DocumentReleaseQuery query) {
        List<DocumentReleaseBO> documentReleaseBOS = DocumentReleaseConverter.fromPO(documentReleaseDao.listDocumentReleaseById(query));
        documentReleaseBOS = changeEffectFlag(documentReleaseBOS);
        return documentReleaseBOS;
    }

    @Override
    public List<DocumentReleaseBO> listDocumentRelease(DocumentReleaseQuery query) {
        List<DocumentReleaseBO> documentReleaseBOS = DocumentReleaseConverter.fromPO(documentReleaseDao.listDocumentRelease(query));
        documentReleaseBOS = changeEffectFlag(documentReleaseBOS);
        return documentReleaseBOS;
    }

    @Override
    public List<DocumentReleaseBO> listAllDocumentRelease(DocumentReleaseQuery query) {
        List<DocumentReleaseBO> documentReleaseBOS = DocumentReleaseConverter.fromPO(documentReleaseDao.listAllDocumentRelease(query));
        documentReleaseBOS = changeEffectFlag(documentReleaseBOS);
        return documentReleaseBOS;
    }

    @Override
    public List<DocumentReleaseBO> listDocumentReleaseByReleaseId(Collection<Long> collection) {
        return DocumentReleaseConverter.fromPO(documentReleaseDao.listDocumentReleaseByReleaseId(collection));
    }

    /**
     * 查找多个
     *
     * @param query 查找条件
     */
    @Override
    public int countDocumentReleaseByFlowId(DocumentReleaseQuery query) {
        return documentReleaseDao.countDocumentReleaseByFlowId(query);
    }

    //查全部个数
    @Override
    public int countDocumentRelease(DocumentReleaseQuery query) {
        return documentReleaseDao.countDocumentRelease(query);
    }

    //查全部个数
    @Override
    public int countAllDocumentRelease(DocumentReleaseQuery query) {
        return documentReleaseDao.countAllDocumentRelease(query);
    }


    /**
     * 生效
     *
     * @param query
     */
    @Override
    public DocumentReleaseBO takeEffectDocumentRelease(DocumentReleaseQuery query) {
        DocumentReleasePO documentReleasePO = DocumentReleaseConverter.toPO(query);
        int row = documentReleaseDao.takeEffectDocumentRelease(documentReleasePO);
        if (row == 0) {
            return null;
        }
        changeEffectFlag(Collections.singletonList(DocumentReleaseConverter.fromPO(documentReleasePO)));
        return DocumentReleaseConverter.fromPO(documentReleasePO);
    }

    /**
     * 失效
     *
     * @param query
     */
    @Override
    public DocumentReleaseBO loseEffectDocumentRelease(DocumentReleaseQuery query) {
        DocumentReleasePO documentReleasePO = DocumentReleaseConverter.toPO(query);
        int row = documentReleaseDao.loseEffectDocumentRelease(documentReleasePO);
        if (row == 0) {
            return null;
        }
        changeEffectFlag(Collections.singletonList(DocumentReleaseConverter.fromPO(documentReleasePO)));
        return DocumentReleaseConverter.fromPO(documentReleasePO);
    }

    /**
     * 文件生效
     * @param query
     * @return
     */
    @Override
    public int takeEffectFlagDocumentRelease(DocumentReleaseQuery query) {
        DocumentReleasePO documentReleasePO = DocumentReleaseConverter.toPO(query);
        return documentReleaseDao.takeEffectFlagDocumentRelease(documentReleasePO);
    }

    /**
     * 文件失效
     * @param query
     * @return
     */
    @Override
    public int loseEffectFlagDocumentRelease(DocumentReleaseQuery query) {
        DocumentReleasePO documentReleasePO = DocumentReleaseConverter.toPO(query);
        return documentReleaseDao.loseEffectFlagDocumentRelease(documentReleasePO);
    }

    /**
     * 文件生效
     * @param query
     * @return
     */
    @Override
    public int takeEffectFlagDocumentReleaseById(DocumentReleaseQuery query) {
        return documentReleaseDao.takeEffectFlagDocumentReleaseById(query.getDocumentReleaseIdS());
    }

    /**
     * 文件失效
     * @param query
     * @return
     */
    @Override
    public int loseEffectFlagDocumentReleaseById(DocumentReleaseQuery query) {
        return documentReleaseDao.loseEffectFlagDocumentReleaseById(query.getDocumentReleaseIdS());
    }

    /**
     * 改变生效状态
     */
    private List<DocumentReleaseBO> changeEffectFlag(List<DocumentReleaseBO> documentReleaseBOS) {
        List<Long> collect = documentReleaseBOS.stream()
                .filter(d -> {
                    if (Objects.isNull(d.getEffectDate())) {
                        return false;
                    }
                    return d.getEffectDate().isBefore(LocalDate.now().plusDays(+1)) && Objects.equals(true, d.getEffectValve());
                })
                .map(DocumentReleaseBO::getDocumentReleaseId)
                .collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(collect)) {
            documentReleaseDao.takeEffectFlagDocumentReleaseById(collect);
            List<DocumentReleaseBO> documentReleaseBOS1 = documentReleaseBOS.stream().filter(d -> collect.contains(d.getDocumentReleaseId())).peek(d -> d.setEffectFlag(true)).collect(Collectors.toList());
            //取差集 bos - bos1
            documentReleaseBOS = documentReleaseBOS.stream().filter(item -> !documentReleaseBOS1.contains(item)).collect(toList());
            //替换原来的生效状态为false的数据
            documentReleaseBOS.addAll(documentReleaseBOS1);
        }
        return documentReleaseBOS;
    }

}
